/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_mongodb_detect.c                                 */
/*  NOTE      =                                                    */
/*  DATE      = 2016/04/1 by jikw                                  */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include <mongodb/mongodb_core.h>
#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "../../../src/module/mongodb/config.h"

extern int mongodb_detect(Conntrack *p_st_conn, Apm_packet *p_st_packet);

static int __suite_init(void)
{

	return 0;
}

static int __suite_cleanup(void)
{

	return 0;
}

static void case1(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;

    i_ret = mongodb_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_NO);

    return;
}

static void case2(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_packet == NULL)
        goto ERROR;
    memset(p_st_packet, 0x00, sizeof(Apm_packet));
    p_st_packet->p_payload = (char *)"\x3b\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\xd4\x07\x00\x00\x00\x00\x00\x00\x61\x64\x6d\x69\x6e\x2e\x24\x63\x6d\x64\x00\x00\x00\x00\x00\xff\xff\xff\xff\x13\x00\x00\x00\x10\x69\x73\x6d\x61\x73\x74\x65\x72\x00\x01\x00\x00\x00\x00";
    p_st_packet->ui_payload_len = 58;

    i_ret = mongodb_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_YES);

    frfree(p_st_conn);
    frfree(p_st_packet);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_packet)
        frfree(p_st_packet);

    return;
}

static void case3(void)
{
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;
    int i_ret = 0;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_packet == NULL)
        goto ERROR;
    memset(p_st_packet, 0x00, sizeof(Apm_packet));
    p_st_packet->p_payload = (char *)"\x3a\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x61\x64\x6d\x69\x6e\x2e\x24\x63\x6d\x64\x00\x00\x00\x00\x00\xff\xff\xff\xff\x13\x00\x00\x00\x10\x69\x73\x6d\x61\x73\x74\x65\x72\x00\x01\x00\x00\x00\x00";
    p_st_packet->ui_payload_len = 58;

    i_ret = mongodb_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_NO);

    frfree(p_st_conn);
    frfree(p_st_packet);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_packet)
        frfree(p_st_packet);

    return;
}

static void case4(void)
{
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;
    int i_ret = 0;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_packet == NULL)
        goto ERROR;
    memset(p_st_packet, 0x00, sizeof(Apm_packet));
    p_st_packet->p_payload = (char *)"\x4e\x00\x00\x00\xca\x18\x5b\xa8\x04\x65\xd5\xb3\x01\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x2a\x00\x00\x00\x02\x79\x6f\x75\x00\x10\x00\x00\x00\x31\x32\x37\x2e\x30\x2e\x30\x2e\x31\x3a\x33\x38\x37\x30\x39\x00\x01\x6f\x6b\x00\x00\x00\x00\x00\x00\x00\xf0\x3f\x00";
    p_st_packet->ui_payload_len = 78;

    i_ret = mongodb_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_NO);

    frfree(p_st_conn);
    frfree(p_st_packet);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_packet)
        frfree(p_st_packet);

    return;
}

static void case5(void)
{
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;
    int i_ret = 0;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_packet == NULL)
        goto ERROR;
    memset(p_st_packet, 0x00, sizeof(Apm_packet));
    p_st_packet->p_payload = (char *)"\x3a\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\xd4\x07\x00\x00\x00\x00\x00\x00\x61\x64\x6d\x69\x6e\x2e\x24\x63\x6d\x64\x00\x00\x00\x00\x00\xff\xff\xff\xff\x13\x00\x00\x00\x10\x69\x73\x6d\x61\x73\x74\x65\x72\x00\x01\x00\x00\x00\x00";
    p_st_packet->ui_payload_len = 58;

    i_ret = mongodb_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_YES);

    frfree(p_st_conn);
    frfree(p_st_packet);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_packet)
        frfree(p_st_packet);

    return;
}

int add_suite_mongodb_detect(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" mongodb_detect()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", case1) == NULL ||
        CU_add_test(pSuite, "case2", case2) == NULL ||
        CU_add_test(pSuite, "case3", case3) == NULL ||
        CU_add_test(pSuite, "case4", case4) == NULL ||
        CU_add_test(pSuite, "case5", case5) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
